import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.1
import com.View.TreeModel 1.0

Item {
    property TreeModel branchStat
    property var activeRow
    property var tabName

    //    signal doubleClick(object row)
    signal rightClick(int x, int y)
    signal leftClick(int row)

    ColumnLayout {
        anchors.fill: parent
        TreeView {
            id: gridBranchStat
            anchors.left: parent.left
            anchors.leftMargin: 5
            anchors.top: parent.top
            anchors.topMargin: 5
            //            anchors.bottom: parent.bottom
            //            anchors.bottomMargin: 30
            anchors.right: parent.right
            anchors.rightMargin: 5
            Layout.fillHeight: true
            //            rowDelegate: rowDele

            //            headerVisible: false
            //            backgroundVisible: false
            model: branchStat //g_MainQuo.getTreeMenu()

            TableViewColumn {
                title: "代理机构编号"
                role: "branchID"
                resizable: true
                width: 110
            }
            TableViewColumn {
                title: "代理机构名称"
                role: "branchName"
                resizable: true
                width: 120
            }
            TableViewColumn {
                title: "相对强平客户数"
                role: "LV3Count"
                resizable: true
                width: 80
            }
            TableViewColumn {
                title: "相对强平客户百分比"
                role: "LV3Percent"
                resizable: true
                width: 80
            }
            TableViewColumn {
                title: "绝对强平客户数"
                role: "LV4Count"
                resizable: true
                width: 80
            }
            TableViewColumn {
                title: "绝对强平客户百分比"
                role: "LV4Percent"
                resizable: true
                width: 80
            }
            TableViewColumn {
                title: "追保客户数"
                role: "LV2Count"
                resizable: true
                width: 60
            }
            TableViewColumn {
                title: "追保客户百分比"
                role: "LV2Percent"
                resizable: true
                width: 80
            }
            TableViewColumn {
                title: "追保金额"
                role: "CallBalance"
                resizable: true
                width: 90
            }
            TableViewColumn {
                title: "总浮动盈亏"
                role: "TotalPL"
                resizable: true
                width: 70
            }
            TableViewColumn {
                title: "总客户数"
                role: "CustCount"
                resizable: true
                width: 60
            }
            onDoubleClicked: {
                g_MainQuo.openRiskCustList(currentIndex)
                //                if (branchStat.isLeafNode(currentIndex)) {

                //                    //                    else
                //                    //打开风险客户
                //                } else {
                //                    if (isExpanded(currentIndex))
                //                        collapse(currentIndex)
                //                    else
                //                        expand(currentIndex)
                //                }
            }

            onClicked: {
                activeRow = currentIndex
                if (branchStat.isLeafNode(currentIndex)) {
                    console.debug("click Leaf")
                    //                    g_MainQuo.openMenu(currentIndex)
                } else
                    console.debug("click not Leaf")
            }

            Component.onCompleted: {
                branchStat = g_MainQuo.getBranchStat()
                //展开树
                var nRow = branchStat.rowCount(gridBranchStat.rootIndex)
                console.debug("Tree rowCount " + nRow)
                for (var i = 0; i < nRow; ++i) {
                    var leve1Index = branchStat.index(i, 0,
                                                      gridBranchStat.rootIndex)
                    gridBranchStat.expand(leve1Index) //1级节点一般都是非叶子节点，直接展开

                    var nLevel1RowCount = branchStat.rowCount(leve1Index)
                    console.debug("Tree1 rowCount " + nLevel1RowCount)
                    for (var k = 0; k < nLevel1RowCount; ++k) {
                        var leve2Index = branchStat.index(k, 0, leve1Index)
                        if (!branchStat.isLeafNode(leve2Index))
                            gridBranchStat.expand(leve2Index)
                    }
                }

                clicked(gridBranchStat.rootIndex)
            }
        }

        RowLayout {
            Layout.preferredHeight: 40
            Text {
                anchors.left: parent.left
                anchors.leftMargin: 25
                text: qsTr("提示：双击可查看该代理下的风险客户")
            }

            Button {
                text: "刷新子代理"
                //                Layout.alignment: Qt.AlignCenter
                anchors.left: parent.left
                anchors.leftMargin: parent.width / 2
                onClicked: {
                    if (g_MainQuo.dbClickBranchStat(
                                gridBranchStat.currentIndex)) {
                        var parentNode = branchStat.parent(
                                    gridBranchStat.currentIndex)
                        //                        var cur = currentIndex
                        gridBranchStat.collapse(parentNode)
                        gridBranchStat.expand(parentNode)
                        gridBranchStat.expand(gridBranchStat.currentIndex)
                        //                        clicked(cur)
                    }
                }
            }
            Item {
                Layout.fillWidth: true
            }

            //            Item {
            //                Layout.preferredWidth: 25 //fillWidth把最后一个挤出去的bug补救
            //            }
        }
    }
    Button {
        text: "关闭"
        anchors.right: parent.right
        anchors.rightMargin: 25
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 10
        onClicked: closeTab(tabName)
    }
    Component {
        id: rowDele
        Rectangle {
            id: rect
            height: 28
            //            color:"blue"
            color: styleData.selected ? "pink" : "white"
            //            color: "transparent";
            MouseArea {
                anchors.fill: parent
                acceptedButtons: Qt.RightButton //Qt.LeftButton |
                onClicked: {
                    //                    clicked();
                    //                    rowDele.parent.cliecked(styleData.row);
                    if (mouse.button == Qt.RightButton) {

                        //                        leftClick(styleData.row);
                        //                        rightClick(styleData.row)
                        rightClick(mouse.x, mouse.y)
                        console.log(typeof styleData)
                    } else if (mouse.button == Qt.LeftButton) {
                        console.log("ListFrame -onClicked LeftButton " + styleData.selected)
                        leftClick(styleData.row)
                    }
                }
                //                onDoubleClicked: {
                //                    console.log("ListFrame -onDoubleClicked")
                //                    doubleClick(styleData.row)
                //                }

                //                onPressed: {
                //                    console.log("ListFrame -onClicked onPressed")
                //                }
            }
        }
    }

    Menu {
        // 右键菜单
        id: rBtnMenu

        MenuItem {
            text: qsTr("查看风险客户")
            onTriggered: {
                console.log("do -查看风险客户")
                g_MainQuo.openRiskCustList(activeRow)
            }
        }
    }

    onRightClick: {

        //                currentIndex = row

        //        gridBranchStat.selection.select()tCurrentIndex()lect()currentIndex = row.index
        //        activeRow = branchStat.parent(activeRow) //i
        //        activeRow = branchStat.parent(activeRow)
        //        activeRow = branchStat.parent(branchStat.index(row, 0))
        //        activeRow = gridBranchStat.rootIndex.child(row, 0)
        console.log("onRightClick " + x + " " + y)

        activeRow = gridBranchStat.indexAt(x, y)
        //        gridBranchStat.selection.select(activeRow)//TypeError: Cannot call method 'select' of null
        //        gridBranchStat.clicked(activeRow)
        console.log("grid -onClicked " + activeRow)
        rBtnMenu.popup()
    }
}
